<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);

$url = $_POST['url'];
    
if (empty($url)) {
    $url = 'http://html.com/manager/iphone/download/info';
}

$body = $_POST['body'];

if (empty($body)) {
    $body='{
"currentVersion": "1.1",
"code":"Buy",
"device":"iphone"
}';
    }
    
if (!empty($_POST)) {   

    $response = do_post_request($url, json_decode($body));
    if (isset($response{pow(2, 16) + 1000}))
	{
        header('Pragma: public'); // required
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private', false); // required for certain browsers
        header('Content-Type: application/force-download');
        header('Content-Disposition: attachment; filename=download.zip;');
        header("Content-Transfer-Encoding: binary");
        header('Content-Length: ' . strlen($response));
        ob_clean();
        flush();
    }
	
	echo $response;
	die;
}

function do_post_request($url, $postdata, $optional_headers = null) {
    //Collect Postdata 
	$data = ""; 
    $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 
	
    foreach($postdata as $key => $val) 
    { 
        $data .= "--$boundary\n"; 
        $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
     } 
    $data .= "--$boundary\n"; 

    $params = array('http' => array(
            'method' => 'POST',
            'header'=>"SECRETKEY: adsf!@##132\r\n",
            'content' => $data
            ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
	
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        echo ("Problem with $url, $php_errormsg");die;
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
        echo ("Problem reading data from $url, $php_errormsg");die;
    }
    return $response;
}

function sendPost($url, $postData) {
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_exec ($ch);
    curl_close ($ch);
    return true;
}

?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="charset=utf-8">
    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
            Submit URL: <input type="text" size="100" name="url" value="<?php echo htmlspecialchars($url);?>" />
            <textarea name="body" style="width:100%;height:50%;"><?php echo htmlspecialchars($body); ?></textarea>
            <input type="submit" value="Submit">
        </form>
        <div>
            <pre>
                <?php echo htmlspecialchars($response); ?>
            </pre>
        </div>
    </body>
</html>